package com.gtanyin.demo.televisionplus

import android.content.Intent
import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.design.widget.Snackbar
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.widget.LinearLayoutManager
import android.view.Menu
import android.view.MenuItem
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.gtanyin.demo.televisionplus.Items.LiveItem
import com.qzb.common.base.BaseActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
import kotlinx.android.synthetic.main.content_main.*

class MainActivity : BaseActivity<Contract.Presenter, Contract.Model>(), NavigationView.OnNavigationItemSelectedListener {

    private lateinit var adapter: MainListAdapter
    private var mData = ArrayList<LiveItem>()

    override fun getLayoutId(): Int = R.layout.activity_main

    override fun initPresenter() {
    }

    override fun initView() {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setSupportActionBar(toolbar)

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }

        val toggle = ActionBarDrawerToggle(
                this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()

        nav_view.setNavigationItemSelectedListener(this)

        adapter = MainListAdapter(mData)
        recycler_view.layoutManager = LinearLayoutManager(mContext)
        recycler_view.adapter = this.adapter
        initData()
        refresh_layout.setOnRefreshListener { mHandler.postDelayed({ refresh_layout.isRefreshing = false }, 2000) }

        initEvents();
    }

    private fun initEvents() {
        adapter.setOnItemClickListener({ _: BaseQuickAdapter<Any, BaseViewHolder>, _: View, i: Int ->
            val intent=Intent(mContext,PlayActivity::class.java)
            intent.putExtra("title",mData[i].name)
            intent.putExtra("url",mData[i].url)
            startActivity(intent)
        })
    }


    private fun initData() {
        mData.add(LiveItem("台视HD", "https://res.exexm.com/cw_145225549855002"))
        mData.add(LiveItem("凤凰卫视", "rtmp://live.hkstv.hk.lxdns.com:1935/live/hks"))
        adapter.notifyDataSetChanged()
    }


    class MainListAdapter(ddata: List<LiveItem>) : BaseQuickAdapter<LiveItem, BaseViewHolder>(ddata) {

        init {
            mLayoutResId = R.layout.item_live
        }

        override fun convert(helper: BaseViewHolder?, item: LiveItem?) {
            helper!!.setText(R.id.name, item!!.name)
        }

    }

    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        when (item.itemId) {
            R.id.nav_camera -> {
                // Handle the camera action
            }
            R.id.nav_gallery -> {

            }
            R.id.nav_slideshow -> {

            }
            R.id.nav_manage -> {

            }
            R.id.nav_share -> {

            }
            R.id.nav_send -> {

            }
        }

        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }
}
